
package hardwareToSQL;

import java.util.Hashtable;
import java.util.LinkedList;


public class InputController extends Thread{

	/**Object collecting the data from the hardware */
	private DataCollector hardware;
	/**Object sending the data to the SQL database */
	private InsertSQL toSQL;

	/**Variable indicating that the input comes from the hardware */
    @SuppressWarnings("unused")
	private boolean normalHardwareInput = true;	
    /**Variable indicating that the input is test data coming in directly */
    private boolean testInputDirect;	
    /**Variable indicating that the input is test data coming from a modem */
    private boolean testInputModem;	
    /**Variable indicating that the input is simulated data */
    private boolean simulationInput;	
	
    /**Buffer containing test data, if tests are being run */
    private LinkedList<Character> buff;
    
	public int bytesReceived;
	public int dataTrunkReceived;
	public Hashtable<String,Integer> timesRowUpdated;

	/**Class constructor */
	public InputController(){
		
	}

	/**Constructor used if input is test data coming in directly,
	 * i.e., with no use of modem */
	public InputController(LinkedList<Character> buff){
		testInputDirect = true;
		this.buff = buff;
	}
	
	/**Constructor used if input is test data coming in through modem,
	 * or through a simulator */
	public InputController(boolean testInputModem,
			boolean simulationInput){
		this.testInputModem = testInputModem;
		this.simulationInput = simulationInput;
	}
	
	/**Main method(execute this from the shell...) */
	public static void main(String[]args){
		InputController l = new InputController();
		l.start();
	}

	/**Start the transfer of data from the hardware to the SQL */
	public void run() {

		/**Run the data collector in a new thread */
		if(testInputDirect){
			hardware = new DataCollector(buff);
		} else if (testInputModem){
			hardware = new DataCollector();
		} else if (simulationInput){
			hardware = new DataCollector();
		} else {
			hardware = new DataCollector();
		}
		hardware.start();
		
		/**Set up the class for processing the input strings to SQL */
		toSQL = new InsertSQL();
		
		while(true){
			//System.out.println("time is (in millisec):"+(new Date()).getTime()); 
			toSQL.takeNextString(hardware.getNextData());
			bytesReceived = hardware.bytesReceived;
			dataTrunkReceived = hardware.dataReceived;
			timesRowUpdated = toSQL.timesRowUpdated();
		}
		
	}
}
